package com.icez.base_util_library

import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.contentValuesOf
import androidx.fragment.app.Fragment
import com.icez.base_util_library.activityResultContracts.TakePictureResultContracts
import java.io.File
import java.net.URI

/**
 *  意图工具类
 * @author icez
 */
class IntentUtil {
    companion object {
        val instance: IntentUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            IntentUtil()
        }
    }

    /**
     *  跳转Activity
     * @param context 上下文
     * @param intent 意图
     */
    public inline fun <reified T> toActivity(context: Context?, intent: Intent) {
        if (context != null) {
            intent.setClass(context,T::class.java)
        }
        context?.startActivity(intent)
    }

    /**
     *  跳转Activity，并使用setResult(params)，然后Finish后，回调参数; .launch(Intent)调用
     * @param activity Activity
     * @param mOnCallbackResult 数据回调
     */
    public fun toActivityForResult(
        activity: AppCompatActivity?,
        mOnCallbackResult: ((activityResult: ActivityResult) -> Unit)
    ): ActivityResultLauncher<Intent>? {
        return activity?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            mOnCallbackResult(it)
        }
    }

    /**
     *  跳转Activity，并使用setResult(params)，然后Finish后，回调参数; .launch(Intent)调用
     * @param fragment Activity
     * @param mOnCallbackResult 数据回调
     */
    public fun toActivityForResult(
        fragment: Fragment?,
        mOnCallbackResult: ((activityResult: ActivityResult) -> Unit)
    ): ActivityResultLauncher<Intent>? {
        return fragment?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            mOnCallbackResult(it)
        }
    }


    /**
     *  关闭Activity并回调参数
     * @param activity Activity
     * @param intent 意图
     * @param code 返回的code
     */
    public fun activityCallbackResult(
        activity: AppCompatActivity?,
        intent: Intent,
        code: Int = Activity.RESULT_OK
    ) {
        activity?.setResult(code, intent)
        activity?.finish()
    }

    /**
     *  拍照
     * @param activity Activity
     * @param mOnTakePictureListener 回调监听
     */
    public fun toTakePictureForFragment(
        fragment: Fragment?,
        mOnTakePictureListener: (sdPath: String?, appUri: Uri?) -> Unit
    ):ActivityResultLauncher<String?>? {
        return fragment?.registerForActivityResult(TakePictureResultContracts()) {
            if (it != null) {
                var realPath = FileUtil.instance.getSDFile()?.absolutePath+File.separator+Environment.DIRECTORY_PICTURES+it?.path?.substringAfterLast(Environment.DIRECTORY_PICTURES)
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
                    realPath = ""
                    mOnTakePictureListener(realPath,it)
                }else{
                    //文件是找不到的，就会全局去找，这样它就全局扫描查找，也就可以从数据库中查询出数据
                    MediaScannerConnection.scanFile(
                        fragment.requireContext(),
                        arrayOf(it?.path),
                        arrayOf("image/*"),
                        object :
                            MediaScannerConnection.OnScanCompletedListener {
                            override fun onScanCompleted(path: String?, uri: Uri?) {

                                mOnTakePictureListener(realPath,it)
                            }
                        })
                }


            }
        }
    }


    /**
     *  拍照
     * @param activity Activity
     * @param mOnTakePictureListener 回调监听
     */
    public fun toTakePicture(
        activity: AppCompatActivity?,
        mOnTakePictureListener: (sdPath: String?, appUri: Uri?) -> Unit
    ): ActivityResultLauncher<String?>? {
        return activity?.registerForActivityResult(TakePictureResultContracts()) {
            if (it != null) {
                var realPath = FileUtil.instance.getSDFile()?.absolutePath+File.separator+Environment.DIRECTORY_PICTURES+it?.path?.substringAfterLast(Environment.DIRECTORY_PICTURES)
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
                    realPath = ""
                    mOnTakePictureListener(realPath,it)
                }else{
                    //文件是找不到的，就会全局去找，这样它就全局扫描查找，也就可以从数据库中查询出数据
                    MediaScannerConnection.scanFile(
                        activity,
                        arrayOf(it?.path),
                        arrayOf("image/*"),
                        object :
                            MediaScannerConnection.OnScanCompletedListener {
                            override fun onScanCompleted(path: String?, uri: Uri?) {

                                mOnTakePictureListener(realPath,it)
                            }
                        })
                }


            }
        }
    }
}